package org.yjc.controller;

import java.util.HashMap;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.yjc.custom.JqgridParam;
import org.yjc.custom.JqgridUtils;
import org.yjc.entity.TbClass;
import org.yjc.form.TbClassForm;
import org.yjc.service.TbClassService;

@Controller
@RequestMapping(value="/tbclass")
public class TbClassController {
	@Autowired
	private TbClassService tbclassservice;
	
	@RequestMapping(value="/tbclassmain")
	public String allclass() {
		return "tbclass/tbclassmain";
	}
	
	@RequestMapping(value="/list",method=RequestMethod.POST)
	@ResponseBody
	public Object list(JqgridParam param) {
		Pageable pageable = param.buildPageable();
		Page<TbClass> page = null;
		if(!param.getSearch()) {
			page=tbclassservice.findAll(pageable);
		}else {
			page=tbclassservice.findAll(param.buildSpec(), pageable);
		}
		HashMap<String, Object> result = JqgridUtils.getPageResult(page);
		return result;
	}
	
	@RequestMapping(value="/edit")
	@ResponseBody
	public Object edit(String id,TbClassForm form) {
		String oper = form.getOper();
		if("del".equals(oper)) {
			tbclassservice.deleteById(Integer.parseInt(id));
		}else if("add".equals(oper)) {
			TbClass tbclass = new TbClass();
			BeanUtils.copyProperties(form, tbclass);
			tbclassservice.save(tbclass);
		}else if("edit".equals(oper)) {
			TbClass tbclass = new TbClass();
			tbclass.setId(Integer.parseInt(id));
			BeanUtils.copyProperties(form, tbclass);
			tbclassservice.save(tbclass);
		}
		return "OK";
		
	}
}
